// 获取用户token的方法
export function getToken() {
	let jwt = uni.getStorageSync('jwt');
	if (!jwt) {
		uni.navigateTo({
			url: '/pages/login/login'
		})
	}
	return jwt;
}

// 格式化时间
export function formatTime(date) {
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');
	const hours = String(date.getHours()).padStart(2, '0');
	const minutes = String(date.getMinutes()).padStart(2, '0');
	const seconds = String(date.getSeconds()).padStart(2, '0');

	return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 格式化日期
export function formatDate(date) {
	const year = date.getFullYear();
	const month = String(date.getMonth() + 1).padStart(2, '0');
	const day = String(date.getDate()).padStart(2, '0');

	return `${year}-${month}-${day}`;
}

// 个位数补0
export function formatNum(n) {
	if (n < 10) {
		return '0' + n;
	} else {
		return n;
	}
}

// 根据时间格式化为更易懂的文字
export function formatTimeToText(time) {
	let now_time = new Date().getTime();
	let time_diff = parseInt((now_time - (new Date(time).getTime())) / 1000);
	if (time_diff < 60) {
		return '刚刚';
	}
	if (time_diff < 3600) {
		return parseInt(time_diff / 60) + '分钟前';
	}
	if (time_diff < 86400) {
		return parseInt(time_diff / 3600) + '小时前';
	}
	if (time_diff < 2592000) {
		return parseInt(time_diff / 86400) + '天前';
	} else {
		return time;
	}
}